﻿using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using Microsoft.CSharp;
using System.Text;

namespace Wicresoft.EKA.NRemedy.CodeGenerate
{
    public class CSharpModelGenerator
    {
        private CodeGeneratorOptions _options;
        private ARSchema asrsch;


        public CSharpModelGenerator(ARLoginContext context)
        {
            asrsch = new ARSchema(context);
            _options = new CodeGeneratorOptions();
            _options.BlankLinesBetweenMembers = false;
            _options.IndentString = "\t";
        }

        public string GenerateClassByFormName(string namespacem, string FormName, List<uint> ids, CSharpModelGeneratorDefaultFactory csmgdf)
        {
            CodeCompileUnit ccu = null;
            CSharpModelGenerateCode cmgc = new CSharpModelGenerateCode();

            string koModelName = FormName.Replace(" ", "_") + "ViewModel";

            GlobalVar.JScolNames = new StringBuilder(" \n /*ColNames JS   \n \n colNames: [");
            GlobalVar.JS = new StringBuilder(" \n /*Model JS  \n \n colModel: [\n");
            GlobalVar.JSLocal = new StringBuilder(" \n /*Local Model JS  \n \n colModel: [\n");
            GlobalVar.koModel = new StringBuilder(" \n /*KO Model JS  \n \n function " + koModelName +
                "(data) {\n var self = this;\n var modelJSON=data.Model;\n");
            GlobalVar.koModelCreate = new StringBuilder(" \n /*KO Model JS Create  \n \n function " + koModelName +
    "() {\n var self = this;\n");
            GlobalVar.koHtml = new StringBuilder(" \n /*KO Html  \n \n \n");
            ccu = cmgc.GenerateClassByFormName(asrsch, namespacem, FormName, ids, csmgdf);
            CSharpCodeProvider provider = new CSharpCodeProvider();
            StringWriter sw = new StringWriter();
            string code = string.Empty;
            try
            {
                provider.GenerateCodeFromCompileUnit(ccu, sw, _options);
                sw.Flush();
                code = sw.GetStringBuilder().ToString();
                GlobalVar.JScolNames.Append("],");
                GlobalVar.JS.AppendLine("  ], ");
                GlobalVar.JSLocal.AppendLine("  ], ");

                GlobalVar.koModel.AppendLine("}\n ");
                GlobalVar.koModel.AppendLine("$.getJSON( \"url\",{RequestID:\"111111\"},function(data, textStatus, jqXHR) { // RequestID is Qualification");
                GlobalVar.koModel.AppendLine("ko.applyBindings(new " + koModelName + "(data));");
                GlobalVar.koModel.AppendLine("});");

                GlobalVar.koModelCreate.AppendLine("}\n ");

                GlobalVar.JScolNames.AppendLine(" \n*/");
                GlobalVar.JS.AppendLine(" */");
                GlobalVar.JSLocal.AppendLine(" */");
                GlobalVar.koModel.AppendLine(" */");
                GlobalVar.koModelCreate.AppendLine(" */");
                GlobalVar.koHtml.AppendLine(" */");

                return code + GlobalVar.JScolNames.ToString() + GlobalVar.JSLocal.ToString() + GlobalVar.JS.ToString()
                    + GlobalVar.koModel.ToString() + GlobalVar.koModelCreate.ToString() + GlobalVar.koHtml.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sw.Close();
            }

            return string.Empty;
        }

    }
}
